package my.georings.expandable;

import my.georings.R;
import my.georings.data.MyLocation;
import my.georings.dialog.VolumeDialog;
import android.app.Activity;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ToggleButton;



public abstract class LocationEditionAbstractActivity extends Activity {

	protected abstract void setListeners();
	protected abstract void setAllViewsToDefault();
	protected abstract void setContentView();
	protected abstract void setCurrentLocation();
	protected abstract void saveChanges();
	
	private VolumeDialog volumeDialog;
	protected MyLocation currentLocation;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.add);
        
        volumeDialog = new VolumeDialog(this, -1);
        
        setContentView();
        setCurrentLocation();
        setAllViewsToDefault();
        setListeners();
    }
	
	

	public void onClick(View v) {
		
		switch(v.getId()){
			
		//// ring
		case R.id.ChangeRingButton:
			Intent i = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
			i.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);
			i.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, false);
			i.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
			if(currentLocation.getRing() != null)
				i.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, currentLocation.getRing());
			startActivityForResult(i, R.id.ChangeRingButton); 
			break;
			
		/////volume
		case R.id.ChangeVolumeButton:
			volumeDialog.setVolume(currentLocation.getVolume());
			volumeDialog.show();
			break;
			
		case R.id.volumeget_Ok:
			currentLocation.setVolume(volumeDialog.getVolume());
			((TextView)findViewById(R.id.TextViewVolumeCurrent)).setText(currentLocation.getVolume()*10+"%");
			volumeDialog.dismiss();
			break;
			
		case R.id.volumeget_cancel:
			volumeDialog.dismiss();
			break;
			
			
		case R.id.ButtonSave:{
			saveChanges();
			break;
		}
			
			
		case R.id.ToggleButtonRing:
			currentLocation.setChangeRing(((ToggleButton)v).isChecked());
			break;
			
			
		case R.id.ToggleButtonVibra:
			currentLocation.setVibra(((ToggleButton)v).isChecked());
			break;
			
		case R.id.ToggleButtonVolume:
			currentLocation.setChangeVolume(((ToggleButton)v).isChecked());
			break;
			
		case R.id.ToggleButtonMute:
			boolean mute = ((ToggleButton)v).isChecked();
			currentLocation.setMute(mute);
			changeViewsForMute(mute);
			break;
		}
	}
	
	
	@Override
	protected void onActivityResult (int requestCode, int resultCode, Intent data){
		if(requestCode == R.id.ChangeRingButton && resultCode == RESULT_OK){
			Uri uri = (Uri) data.getExtras().get(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
			currentLocation.setRing(uri);
			((TextView)findViewById(R.id.TextViewRingCurrent)).setText(RingtoneManager.getRingtone(this, uri).getTitle(this));
		}
	}
	
	
	protected void setParentListeners(OnClickListener listener){
		
		findViewById(R.id.ChangeVolumeButton).setOnClickListener(listener);
		findViewById(R.id.ChangeRingButton).setOnClickListener(listener);
		findViewById(R.id.ButtonSave).setOnClickListener(listener);
		
		findViewById(R.id.ToggleButtonRing).setOnClickListener(listener);
		findViewById(R.id.ToggleButtonVibra).setOnClickListener(listener);
		findViewById(R.id.ToggleButtonVolume).setOnClickListener(listener);
		findViewById(R.id.ToggleButtonMute).setOnClickListener(listener);
		
		volumeDialog.findViewById(R.id.volumeget_Ok).setOnClickListener(listener);
		volumeDialog.findViewById(R.id.volumeget_cancel).setOnClickListener(listener);
	}
	
	protected void changeViewsForMute(boolean mute){
		((ToggleButton)findViewById(R.id.ToggleButtonVolume)).setEnabled(!mute);
		((Button)findViewById(R.id.ChangeVolumeButton)).setEnabled(!mute);
		
		((ToggleButton)findViewById(R.id.ToggleButtonRing)).setEnabled(!mute);
		((Button)findViewById(R.id.ChangeRingButton)).setEnabled(!mute);
	}
	
	

}
